package com.sunwoda.springbootshiro.realm;

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

/**
 * @program: springboot-shiro
 * @description: 自定义realm
 * @author: SGR
 * @create: 2021-09-27 09:48
 **/
@Slf4j
public class MyRealm extends AuthorizingRealm {
    
    /** 
    * @Description: 授权
    * @Param:  
    * @return:  
    * @Author: SGR 
    * @Date: 2021-09-27 
    */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    /** 
    * @Description: 认证
    * @Param:  
    * @return:  
    * @Author: SGR 
    * @Date: 2021-09-27 
    */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String principal = (String) token.getPrincipal();
        if ("zhang".equals(principal)){
            return new SimpleAuthenticationInfo(principal,token.getCredentials(),this.getName());
        }
        return null;
    }
}
